

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/collection_model/my_collection_list_by_collection_id_head_model.dart';
import 'package:yysk_app/model/collection_model/my_collection_list_by_collection_id_model.dart';

import 'package:yysk_app/net/api/product_api.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/utils/js_tool.dart';



class MyCollectionListController extends GetxController {
  final isRead = true.obs;
    final myCollectionList = RxList<MyCollectionListByCollectionIdItem>([]);
    final headDetail = Rx<MyCollectionListByCollectionIdHeadModel?>(null);
  int pageNum = 1;
  int pageSize = 10;
  final txt = '批量转赠'.obs;
  late String? marketType;
  late String? id;
  final currentTab = 'collection'.obs;
  final type = ''.obs;

  final ScrollController scrollController = ScrollController();
  late RxString collectionId = ''.obs;

  @override
  void onInit() {
    super.onInit();
    
    collectionId.value =Get.parameters['id']?? Get.arguments['id'];
    getCollectionListByCollectionId();
    getCollectionListByCollectionIdHead();
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
         getCollectionListByCollectionId(loadMore: true);
      }
    });
  }

  @override
  void onClose() {
    super.onClose();
    Get.delete<ScrollController>(); //删除控制器实例
    scrollController.dispose();
  }
  //藏品列表
  getCollectionListByCollectionId({loadMore = false}) {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      http.netFetch<MyCollectionListByCollectionIdModel>(
        ProductApi.collectionListByCollectionId,
        params: {'pageNum': pageNum, 'pageSize': 1000,'collectionId':collectionId.value},
        method: Method.post,
        success: (result) {
          List<MyCollectionListByCollectionIdItem> items =result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            myCollectionList.clear();
          }
             myCollectionList.addAll(items);
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNum--;
      }
    }
}
  //头部数据
  getCollectionListByCollectionIdHead() {

   http.netFetch<MyCollectionListByCollectionIdHeadModel>(
        ProductApi.userCollectionDetailHead,
        params: {'collectionId':collectionId.value},
        method: Method.post,
        success: (result) {
          headDetail.value=result.data;
          // consoleLog('${headDetail.value?.collectionEquity}===h44444');
        },
      );
}
}